//
//  LXCustomActionSheet.m
//  KeHuWang
//
//  Created by linxian on 14-4-28.
//  Copyright (c) 2014年 YLife. All rights reserved.
//

#define ISDEVICE7 ([UIDevice currentDevice].systemVersion.intValue >= 7)
#import "LXCustomActionSheet.h"

#define NavBarHeight                    40
#define ButtonHeight                    60
#define ViewHeight                      [UIScreen mainScreen].bounds.size.height


@interface LXCustomActionSheet(){
@private
    float _customViewHeight;
}

- (void) clickButtonAtIndex:(UIButton *)sender;
@end


@implementation LXCustomActionSheet

@synthesize customView, customTitle, buttonTitle, selectedArray, messageArray, asTableView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
}
-(id)initWithTitle:(NSString *)title withIconArray:(NSArray *)iconArray withMsgArray:(NSArray *)msgArray withBtnTitle:(NSString *)btnTitle withSelectArray:(NSArray *)selectArray;
{
    self = [super init];
    if (self) {
        messageArray = msgArray;
        _customViewHeight = msgArray.count * 40.0f;
        // 1.
        self.customTitle = title;
    
        // 2.
        _iconArray = iconArray;
        // 3.
        _msgArray = msgArray;
        // 4.
        if (btnTitle != nil) {
            buttonTitle = btnTitle;
            _customViewHeight += ButtonHeight;
            
            // 5.
            selectedArray = [NSMutableArray arrayWithArray:selectArray];
        }
        
        
        if (ISDEVICE7) {
            customView = [[UIView alloc] initWithFrame:CGRectMake(0, ViewHeight -  _customViewHeight - 44 - 20, 320, _customViewHeight)];
        } else {
            customView = [[UIView alloc] initWithFrame:CGRectMake(0, ViewHeight - _customViewHeight - 44, 320, _customViewHeight)];
        }
    }
        return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    _navBar = [[UINavigationBar alloc] init];
    if (ISDEVICE7) {
        _navBar.frame = CGRectMake(0, ViewHeight - _customViewHeight - NavBarHeight-44 -20, 320, NavBarHeight);
    } else {
        _navBar.frame = CGRectMake(0, ViewHeight - _customViewHeight - NavBarHeight-44 , 320, NavBarHeight);
    }
    _navBar.barStyle = UIBarStyleBlackOpaque;
    UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:self.customTitle];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:nil];
    navItem.leftBarButtonItem = leftButton;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"X" style:UIBarButtonItemStyleDone target:self action:@selector(done)];
    navItem.rightBarButtonItem = rightButton;
    NSArray *array = [[NSArray alloc] initWithObjects:navItem, nil];
    [_navBar setItems:array];
    
    [self.superview addSubview:_navBar];
    
    [self.superview addSubview:customView];
    [self addTableView];
    [self addButtons];
    
}
#pragma mark - 多选模式下，添加下方按钮
- (void) addButtons
{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, asTableView.frame.size.height+10, 280, 40)];
    btn.backgroundColor = [UIColor blackColor];
    [btn setTitle:self.buttonTitle forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickButtonAtIndex:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = messageArray.count+1;
    [customView addSubview:btn];
    customView.backgroundColor = [UIColor whiteColor];
    
}

- (void) addTableView
{
    asTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, messageArray.count * 40.0f)];
    asTableView.scrollEnabled = NO;
    asTableView.dataSource = self;
    asTableView.delegate = self;
    [customView addSubview:asTableView];
}
#pragma mark - actionSheet的消失事件
- (void) done
{
    [self dismissWithClickedButtonIndex:0 animated:YES];
    [self.delegate actionSheet:self clickedButtonAtIndex:0];
}
#pragma mark - 多选模式下，点击下方按钮的相应事件
- (void) clickButtonAtIndex:(UIButton *)sender
{
    //NSLog(@"点击.第%d行",sender.tag);
    [self.delegate actionSheet:self clickedButtonAtIndex:sender.tag];
    [self done];
}

#pragma mark - tableview的代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40.0f;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return messageArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.buttonTitle == nil) {
        static NSString *cellIdentifier = @"cell";// 单选模式
        LXCustomSCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[LXCustomSCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            
        }
        cell.industryView.image = [UIImage imageNamed:[_iconArray objectAtIndex:indexPath.row]];
        cell.msgLabel.text = [messageArray objectAtIndex:indexPath.row];
        
        return cell;
    } else {
        static NSString *cellIdentifier = @"cell";// 多选模式
        LXCustomFCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[LXCustomFCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            
        }
        cell.industryView.image = [UIImage imageNamed:[_iconArray objectAtIndex:indexPath.row]];
        cell.msgLabel.text = [messageArray objectAtIndex:indexPath.row];
        if ([selectedArray[indexPath.row] isEqualToString:@"1"]) {
            cell.selectView.image = [UIImage imageNamed:@"pass.png"];
        } else {
            cell.selectView.image = nil;
        }
        
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.delegate actionSheet:self clickedButtonAtIndex:indexPath.row+1];
    if (self.buttonTitle == nil) {
        // 单选模式
        
        [self done];
    } else {
        // 多选模式
        
        if ([selectedArray[indexPath.row] isEqualToString:@"1"]) {
            selectedArray[indexPath.row] = @"0";
        } else {
            selectedArray[indexPath.row] = @"1";
        }
        NSLog(@"%@",selectedArray);
        NSArray *rows = [NSArray arrayWithObjects:indexPath, nil];
        [asTableView reloadRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    
}
@end

#pragma mark - 单选Cell
@implementation LXCustomSCell
@synthesize industryView, msgLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self drawSCell];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

-(void)drawSCell
{
    // 行业图标
    industryView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 5, 30, 30)];
    industryView.backgroundColor = [UIColor blackColor];
    [self addSubview:industryView];
    
    // 各行信息
    msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 5, 100, 30)];
    msgLabel.textColor = [UIColor blackColor];
    msgLabel.backgroundColor = [UIColor grayColor];
    msgLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:msgLabel];
    
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

#pragma mark - 复选Cell
@implementation LXCustomFCell
@synthesize industryView, msgLabel, selectView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self drawSCell];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

-(void)drawSCell
{
    // 行业图标
    industryView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 30, 30)];
    industryView.backgroundColor = [UIColor clearColor];
    [self addSubview:industryView];
    
    // 各行信息
    msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 5, 60, 30)];
    msgLabel.textColor = [UIColor blackColor];
    msgLabel.backgroundColor = [UIColor grayColor];
    msgLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:msgLabel];
    
    // 选择图片
    selectView = [[UIImageView alloc] initWithFrame:CGRectMake(270, 5, 30, 30)];
    selectView.backgroundColor = [UIColor clearColor];
    [self addSubview:selectView];
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end



